"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const global_1 = require("../../../config/global");
function registerRewardNode(lf, allReward) {
    lf.register('rewardNode', ({ RectNode, RectNodeModel, h }) => {
        class Node extends RectNode {
            getTitleElement() {
                const { model } = this.props;
                const { x, y } = model;
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                if (properties.rewardList) {
                    // 创建一个文本输入框
                    return h("g", {}, [
                        h('text', {
                            fill: '#000000',
                            fontSize: 16,
                            x: x - 62,
                            y: y - 10 - 20 * properties.rewardList.length,
                            width: 50,
                            height: 25
                        }, 'CC_RewardNode')
                    ]);
                }
            }
            ;
            getAllChoiceList() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const nodeData = model.getData();
                const properties = model.getProperties();
                if (properties.rewardList) {
                    // 遍历choiceList并创建对应的h("rect")元素
                    const rewardElements = nodeData.properties.rewardList.map((reward, index) => {
                        // 在这里可以根据choice对象的属性来设置rect元素的属性
                        return [
                            h("foreignObject", Object.assign(Object.assign({}, style), { id: "reward_textarea", type: "reward", height: 26, width: 70, y: y + 10 - 20 * properties.rewardList.length + 40 * index, x: x + 10 }), [
                                h("textarea", {
                                    style: {
                                        width: "100%",
                                        height: "70%",
                                        resize: "none" // 禁止调整文本框大小
                                    },
                                    value: nodeData.properties.rewardList[index].reward_value,
                                    onInput: (event) => {
                                        let newContent = event.target.value;
                                        properties.rewardList[index].reward_value = newContent;
                                        model.setProperties(properties);
                                    },
                                }),
                            ]),
                            h("foreignObject", Object.assign(Object.assign({}, style), { id: "reward_select", type: "reward", height: 25, width: 90, y: y + 10 - 20 * properties.rewardList.length + 40 * index, x: x - 82 }), [
                                h("select", {
                                    style: {
                                        width: "100%",
                                        height: "25px",
                                    },
                                    value: nodeData.properties.rewardList[index].reward_name,
                                    onChange: (event) => {
                                        let newSelection = event.target.value;
                                        properties.rewardList[index].reward_name = newSelection;
                                        model.setProperties(properties);
                                    },
                                }, allReward.map((item, i) => {
                                    return h("option", {
                                        value: item.reward_name,
                                        selected: item.reward_name === nodeData.properties.rewardList[index].reward_name,
                                    }, item.reward_name);
                                }))
                            ])
                        ];
                    });
                    // 返回包含所有choice元素的SVG
                    return h("g", {}, rewardElements);
                }
                return null; // 如果没有choice数据则返回null或其他默认值
            }
            getShape() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const properties = model.getProperties();
                if (properties.rewardList) {
                    return h("g", {}, [
                        h("rect", Object.assign(Object.assign({}, style), { id: "svg_1", height: 30 + 40 * properties.rewardList.length, width: 190, x: x - 190 / 2, y: y - 20 * properties.rewardList.length, fill: "#fd3afa" })),
                        h("rect", Object.assign(Object.assign({}, style), { id: "svg_7", height: 30, width: 190, x: x - 190 / 2, y: y - 30 - 20 * properties.rewardList.length })),
                        this.getTitleElement(),
                        this.getAllChoiceList(),
                        this.getChangeList(),
                    ]);
                }
            }
            getChangeList() {
                const { model } = this.props;
                const { width, height, x, y, } = model;
                const style = model.getNodeStyle();
                const textStyle = model.getTextStyle();
                const { stroke } = model.getNodeStyle();
                const nodeData = model.getData();
                const properties = model.getProperties();
                if (properties.rewardList) {
                    return h("g", {}, [
                        h("rect", Object.assign(Object.assign({}, style), { id: "add", type: "add", height: 16, width: 80, y: y + 3 + 20 * properties.rewardList.length, x: x - 82, fill: "#e8c9b5", onClick: (event) => {
                                const newReward = new global_1.reward('', '', 0);
                                nodeData.properties.rewardList.push(newReward);
                                // 获取当前的Model实例
                                nodeData.text.y += 20;
                                model.initNodeData(nodeData);
                                model.addField();
                            } })),
                        h("svg", {
                            x: x - 50,
                            y: y + 3 + 20 * properties.rewardList.length,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 483.589H544V226.418c0-17.673-14.327-32-32-32s-32 14.327-32 32v257.171H222.83c-17.673 0-32 14.327-32 32s14.327 32 32 32H480v257.17c0 17.673 14.327 32 32 32s32-14.327 32-32v-257.17h257.171c17.673 0 32-14.327 32-32s-14.327-32-32-32z'
                        })),
                        h("rect", Object.assign(Object.assign({}, style), { id: "substract", type: "substract", height: 16, width: 80, y: y + 3 + 20 * properties.rewardList.length, x: x + 5, fill: "#e8c9b5", onClick: (event) => {
                                nodeData.properties.rewardList.pop();
                                nodeData.text.y -= 20;
                                model.initNodeData(nodeData);
                                model.addField();
                            } })),
                        h("svg", {
                            x: x + 40,
                            y: y + 3 + 20 * properties.rewardList.length,
                            width: 15,
                            height: 15,
                            viewBox: '0 0 1126 1024',
                        }, h('path', {
                            fill: stroke,
                            d: 'M801.171 547.589H222.83c-17.673 0-32-14.327-32-32s14.327-32 32-32h578.341c17.673 0 32 14.327 32 32s-14.327 32-32 32z'
                        })),
                    ]);
                }
            }
        }
        class Model extends RectNodeModel {
            constructor(data, graphModel) {
                data.text = {
                    value: (data.text && data.text.value) || '未命名节点-奖励',
                    x: data.x,
                    y: data.y + 90
                };
                super(data, graphModel);
                this.radius = 20;
            }
            initNodeData(data) {
                if (!data.virtual && !data.properties) {
                    const reward = new global_1.RewardNode(allReward.slice(0, 2));
                    data.properties = Object.assign({}, reward);
                    this.width = 190;
                    this.height = 140;
                }
                super.initNodeData(data);
                if (data.properties && data.properties.rewardList) {
                    this.width = 190;
                    this.height = 60 + 40 * data.properties.rewardList.length;
                }
                const onlyPosAsConnect = {
                    message: "连接对应的锚点",
                    validate: (sourceNode, targetNode, sourceAnchor, targetAnchor) => {
                        return sourceAnchor.type === 'right' && targetAnchor.type === 'left';
                    },
                };
                this.sourceRules.push(onlyPosAsConnect);
            }
            addField() {
                this.setAttributes();
                // 为了保持节点顶部位置不变，在节点变化后，对节点进行一个位移,位移距离为添加高度的一半。
                this.move(0, 24 / 2);
                // 更新节点连接边的path
                this.incoming.edges.forEach((egde) => {
                    // 调用自定义的更新方案
                    egde.updatePathByAnchor();
                });
                this.outgoing.edges.forEach((edge) => {
                    // 调用自定义的更新方案
                    edge.updatePathByAnchor();
                });
            }
            getAnchorStyle(anchorInfo) {
                const style = super.getAnchorStyle(anchorInfo);
                if (anchorInfo.type === "left") {
                    style.fill = "red";
                    style.hover.fill = "transparent";
                    style.hover.stroke = "transpanrent";
                    style.className = "lf-hide-default";
                }
                else {
                    style.fill = "green";
                }
                return style;
            }
            setIsShowAnchor() {
                this.isShowAnchor = true;
            }
            getDefaultAnchor() {
                const { width, height, x, y, id } = this;
                return [{
                        x: x + 16 - width / 2,
                        y: y + 16 - height / 2,
                        type: "left",
                        edgeAddable: false,
                        id: `${id}_0`,
                    },
                    {
                        x: x - 16 + width / 2,
                        y: y + 16 - height / 2,
                        type: "right",
                        id: `${id}_1`,
                    },
                ];
            }
        }
        return {
            view: Node,
            model: Model
        };
    });
}
exports.default = registerRewardNode;
